package gsm.cable.server;

import gsm.cable.server.filters.ResponseWraper;
import gsm.cable.server.module.user.User;
import gsm.cable.server.module.user.UsersAction;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.repackaged.org.json.JSONObject;

/**
 * Only servlet that receive all calls from client and send all type of response
 * 
 * @author acer
 * 
 */
public class Controller extends HttpServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = -7474917919738557067L;

	private static final String SIGN_UP = "signup";;

	private static final String SIGN_UP_VERIFICATION = "sign_up_verification";

	private static final String MESSAGE = "message";

	private static final String ERROR_MESSAGE = "error_message";

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO get JSONObject {'signup':{}} corresponding to the 'parameter'
		// key
		try {
			PrintWriter writer = response.getWriter();
			String parameter = request.getParameter("parameter");
			if (parameter != null) {
				JSONObject clientRequest = new JSONObject(parameter);
				if (clientRequest.has(SIGN_UP)) {
					UsersAction.signUp(new User(clientRequest.getJSONObject(SIGN_UP), request.getSession()));
					System.err.println("signup : " + clientRequest);
					response.getWriter().print("{'signup':'true'}");

				} else if (clientRequest.has(SIGN_UP_VERIFICATION)) {
					response.getWriter().print(signUpVerification(request));
				} else {
					writer.print("error : Invalid call to server");
				}
			}
		} catch (Exception e) {
			ResponseWraper responseWraper = (ResponseWraper) response;
			responseWraper.setStatus(HttpServletResponse.SC_EXPECTATION_FAILED, e.getMessage());
			
		}
	}

	private String signUpVerification(HttpServletRequest request) {
		String errorMessage = null;
		String message = null;
		try {
			Object userObj = request.getSession().getAttribute(UsersAction.SIGNUP_INFO);
			if (userObj == null) {
				errorMessage = "Enable to verify user, No Detail found in the current seesion";
			}
			if (UsersAction.verifyMobileCode(new User(new JSONObject(userObj.toString())))) {
				message = "User verified successfully";
			} else {
				errorMessage = "Can not verify the user.";
			}

		} catch (Exception e) {
			errorMessage = "Can not verify the user";
		}

		return errorMessage == null ? "{'" + MESSAGE + "' : '" + message + "'}" : "{'" + ERROR_MESSAGE + "':'" + errorMessage + "'}";
	}

	private HashMap<String, Object> convertToMap(JSONObject object) {
		// TODO Auto-generated method stub
		return null;
	}

	private ArrayList<Object> toArrayList(JSONObject object) {
		// TODO Auto-generated method stub
		return null;

	}

}
